$(function(){
	$(".selector").each(function() {
		 $(this).selectorInit();
	});	
	
	//页面接口权限申请
	var applied = 0;
	var interface_ids = Array();
	var index = 0;
	function search_interface(interface_id){
		var url = 'aj_wpPower.php';
		var action = 'searchInterface';
		var post = {action:action, interface_id:interface_id};
		$.post(url, post, function(json){
			if(1 == json.status){
				$("#interface_result table tbody").remove();
				var html = '<tbody><tr>' + json.info.id + json.info.title + json.info.module_name + json.info.plat_name + json.info.status_txt + json.info.operate + '</tr></tbody>';
				$("#interface_result table").append(html);
				$("#interface_result").show();
				$("#interface_fail").hide();
			}else{
				$("#interface_fail font").text(json.msg);
				$("#interface_fail").show();
				$("#interface_result").hide();
			}
			//添加到申请列表
			$("#addApply").click(function(){
				applied = 0;
				var id = $(this).attr('rel');
				var title = $(this).attr('title');
				var module = $(this).attr('module');
				var platform = $(this).attr('platform');
				var status = $(this).attr('status');
				
				var apply_id = $("#select_already td.apply_id").text();
				
				$("#select_already td.apply_id").each(function(i){
					if(id == $(this).text()){
						applied = 1;
						showCommonLayer('此接口在申请列表中已存在', 1);						
						return;
					}
				});
				if(1 == applied){
					return;
				}
				var power_str = $("input[name='power_str']").val();
				if(-1 != power_str.indexOf(id)){
					showCommonLayer('当前页面已经拥有此接口权限', 1);
					return;
				}
				interface_ids.push(id);
				var html = '<tr>' 
							+ '<td class="apply_id">' + id + '</td>'
							+ '<td>' + title + '</td>'
							+ '<td>' + module + '</td>'
							+ '<td>' + platform + '</td>'
							+ '<td>' + status + '</td>'
							+ '<td><a href="javascript:void(0);" class="deleteApply" rel="' + id + '">删除</a></td>'
							+ '</tr>';
				$("#select_already table tbody").append(html);
				showCommonLayer("添加成功", 4);				
				//从申请列表删除
				$(".deleteApply").click(function(){
					var del_interface = $(this).attr('rel');
					var position = $.inArray(del_interface, interface_ids);
					if(-1 != position){
						interface_ids.splice(position, 1);
					}
					$(this).parent().parent().remove();
				});
			});
		}, 'json');
	}
	$("#search_interface").click(function(){
		var interface_id = $("input[name='interface_id']").val();
		if('' == interface_id){
			showCommonLayer('请输入接口ID', 1);
			return;
		}
		$("select[name='interface'],select[name='i_module'],select[name='i_plat']").val(0);
		search_interface(interface_id);
		return true;
	});
	$("select[name='interface']").change(function(){		
		var interface_id = $(this).val();
		if(0 == interface_id){
			return;
		}
		$("input[name='interface_id']").val('');
		search_interface(interface_id);
		return true;
	});
	
	$("#apply_submit_btn").click(function(){
		var url = 'aj_wpPower.php';
		var action = 'submitApply';
		var webpage_id = $("input[name='webpage_id']").val();	
		var apply_reason = $("textarea[name='apply_reason']").val();
		if('' == apply_reason){
			showCommonLayer('请填写申请原因', 1);
			return;
		}
		var post = {action:action, webpage_id:webpage_id, interface_ids:interface_ids, apply_reason:apply_reason};
		$.post(url, post, function(json){
			if(1 == json.status){
				showCommonLayer('提交成功', 4);
			}else{
				showCommonLayer(json.msg, 5);
			}
		}, 'json');
		return;
	});
	
})
